package com.codermn.demo10;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author jxau_scf
 * @date 2021/6/30 - 16:34
 */
public class Persion {
    private String name;
    private Integer age;

    public Persion() {
    }

    public Persion(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Persion{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void testMethod(String name){
        System.out.println("这是一条测试方法" + name);
    }

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Persion> clazz = Persion.class;
        //获取反射对象的成员变量
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field :
                declaredFields) {
            System.out.println(field);
        }
        System.out.println("=================================");
        //获取反射对象的成员方法
        Method[] methods = clazz.getMethods();
        for (Method method : methods){
            System.out.println(method);

        }
        Persion persion = clazz.newInstance();
        //调用setAge方法
        Method setAge = clazz.getMethod("setAge",Integer.class);
        setAge.invoke(persion, 10);

        System.out.println("=================================");

        //获取构造方法
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor constructor : constructors){
            System.out.println(constructor);
        }
    }
}
